package com.GamersCom.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException;

public class PlatformDAO
{
	protected QueryMgr qm;
	protected Statement stmt;
	
	public PlatformDAO()
	{
		qm = new QueryMgr();
	}
	
	
	public void insert(String query, List paramList) throws SQLException
	{
		qm.executePreparedQuery(query, paramList);
	}
	
	public void delete(String query, List paramList) throws SQLException
	{
		qm.executePreparedQuery(query, paramList);
	}
	
	public void update(String query, List paramList) throws SQLException, MySQLIntegrityConstraintViolationException
	{
		qm.executePreparedQuery(query, paramList);
	}
	
	/**
	 * @param query
	 * @return
	 * @throws SQLException
	 */
	public List<Platform> getAll(String query) throws SQLException
	{
		ResultSet rs = null;
		List<Platform> PlatformList = new ArrayList<Platform>();
		
		rs = qm.fetchList(query);
		// return null, if the resultset is empty
		if ( ! rs.isBeforeFirst() )
		{
			return null;
		}
		
		while (rs.next() ) 
		{
			Platform pf = new Platform();
			pf.setName(rs.getString("name"));
			PlatformList.add(pf);
		}
		
		return PlatformList;
	}
	
	public Platform getPlatform(String query, List paramList) throws SQLException 
	{
		ResultSet rs = qm.fetchList(query, paramList);
		Platform pf = new Platform();
		
		if ( !  rs.first() )
		{
			return null;
		}
		else 
		{
			pf.setName(rs.getString("name"));
		}
		
		return pf;		
	}

	
	
}
